﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
namespace MMMaze
{





    public class PanelMatrix
    {
        public List<LayerGroup> Layers { get; private set; }
        //public PanelMatrix(int LayersCount, int WingCount, double GlobalX, double GlobalY, double GlobalZ, double PanelWidth, double PanelHeigt, Brush PanelDefualtBackGround)
        //{


        //    InitPanelMatrix(LayersCount, WingCount, GlobalX, GlobalY, GlobalZ,  PanelWidth,PanelHeigt ,
        //                    () => new StackPanel()
        //                                        {
        //                                            Width = PanelWidth,
        //                                            Height = PanelHeigt,
        //                                            Background = PanelDefualtBackGround
        //                                        });


        public Dictionary<Panel, Wall> Walls { get; private set; }

        //}

        public Color FogColor
        {
            get;
            set;

        }
        public PanelMatrix(int LayersCount, int WingCount, double GlobalX, double GlobalY, double GlobalZ, double PanelWidth, double PanelHeigt, Func<Panel> PanelCreator, Func<Panel, Panel> PanelDecorator)
        {

            InitPanelMatrix(LayersCount, WingCount, GlobalX, GlobalY, GlobalZ, PanelWidth, PanelHeigt, PanelCreator, PanelDecorator);
        }


        void CorePanel_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            var p = e.OriginalSource as Panel;
            var ps = Walls[p];
            if (ps.Status == WallStatus.Normal)
            {
                ps.Status = WallStatus.InPlayback;
                ps.OrginalProjection = (PlaneProjection)p.Projection.Clone();
                PlaneProjection oldp = p.Projection as PlaneProjection;
                PlaneProjection newp = new PlaneProjection ();
    
                newp.RotationY = 1800;


                p.MoveControlToTempPlace(newp, TimeSpan.FromSeconds(1), () => ps.Status = WallStatus.ShowInTop);
            }
            
        }


        void InitPanelMatrix(int LayersCount, int WingCount, double GlobalX, double GlobalY, double GlobalZ, double PanelWidth, double PanelHeigt, Func<Panel> PanelCreator, Func<Panel, Panel> PanelDecorator)
        {
            this.PanelSize = new Size(PanelWidth, PanelHeigt);
            Layers = new List<LayerGroup>(LayersCount);
            for (var i = 0; i < LayersCount; i++)
            {
                var lyr = new LayerGroup(
                                            WingCount, PanelCreator,
                                            p =>
                                            {
                                                if (PanelDecorator == null) PanelDecorator = Wall.DefaultPanelDecorator;
                                                var tp = PanelDecorator.Invoke(p);
                                                tp.Width = PanelWidth;
                                                tp.Height = PanelHeigt;
                                                return tp;
                                            },
                                            this, 
                                            LayersCount,
                                             i 
                                        )
                {
                    GlobalOffsetZ = GlobalZ - PanelSize.Width * i,
                    GlobalOffsetX = GlobalX,
                    GlobalOffsetY = GlobalY
        

                };
                Layers.Add(lyr);
            }

            Walls = new Dictionary<Panel, Wall>();
            Layers.ForEach
                (
                    l => l.AllWalls.ForEach
                        (
                            w => 
                            {
                                Walls.Add
                                (
                                    w.CorePanel,
                                    w
                                );
                                w.CorePanel.MouseLeftButtonDown += new MouseButtonEventHandler(CorePanel_MouseLeftButtonDown);
                                w.CloseButton.Click += new RoutedEventHandler(CloseButton_Click);
                            }
                        )
                );


        }

        void CloseButton_Click(object sender, RoutedEventArgs e)
        {
            var p = ((Button )e.OriginalSource ).Parent as Panel;
            var ps = Walls[p];
            if (ps.Status == WallStatus.ShowInTop)
            {
                ps.Status = WallStatus.InPlayback;
                p.MoveControlToTempPlace(ps.OrginalProjection, TimeSpan.FromSeconds(1), () => ps.Status = WallStatus.Normal);
            }
        }


        private Size PanelSize { get; set; }

        double _GlobalOffsetX = 0;
        public double GlobalOffsetX
        {
            get
            {
                return _GlobalOffsetX;

            }
            set
            {

                _GlobalOffsetX = value;
                ChangeProperty((l, v) => l.GlobalOffsetX = v, _GlobalOffsetX, false);
            }
        }
        double _GlobalOffsetY = 0;
        public double GlobalOffsetY
        {
            get
            {
                return _GlobalOffsetY;

            }
            set
            {
                _GlobalOffsetY = value;
                ChangeProperty((l, v) => l.GlobalOffsetY = v, _GlobalOffsetY, false);
            }
        }
        double _GlobalOffsetZ = 0;
        public double GlobalOffsetZ
        {
            get
            {
                return _GlobalOffsetZ;

            }
            set
            {
                _GlobalOffsetZ = value;
                ChangeProperty((l, v) => l.GlobalOffsetZ = v, _GlobalOffsetZ, true);
            }
        }

        void ChangeProperty(Action<LayerGroup, double> act, double value, bool Z)
        {
            if (!Z)
                for (var i = 0; i < Layers.Count; i++)
                {
                    act.Invoke(Layers[i], value);
                }
            else
                for (var i = 0; i < Layers.Count; i++)
                {
                    act.Invoke(Layers[i], value - i * PanelSize.Width);
                }
        }


        public void AddTo(Panel Parent)
        {
            foreach (var l in Layers)
            {
                foreach (var w in l.AllWalls)
                {
                    Parent.Children.Add(w.CorePanel );
                }
            };

        }






    }
}
